package com.bw.controller;



import com.alibaba.excel.EasyExcel;
import com.bw.config.UploadMinioUtils;
import com.bw.domain.SysPost;
import com.bw.mapper.SysPostMapper;
import com.bw.service.SysPostService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.bw.utils.Result;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("post")
public class SysPostController {

    @Autowired
    SysPostService sysPostService;


    /**
     * 岗位列表查询分页
     * @param sysPost
     * @param pageNum
     * @return
     */
    @RequestMapping("/list")
    public Result list(@RequestBody SysPost sysPost, @RequestParam(defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,10);
        List<SysPost> list = sysPostService.listAll(sysPost);
        return Result.success(new PageInfo<>(list));
    }
    /**
     *用户岗位下拉
     *
     */
    @RequestMapping("postList")
    public Result postList(){
        List<SysPost>list=sysPostService.postList();
        return Result.success(list);
    }
    /**
     * 岗位添加
     * @param sysPost
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody SysPost sysPost){
        sysPost.setCreateTime(new Date());
        boolean b = sysPostService.save(sysPost);
        return b?Result.success("操作成功"):Result.fail("操作失败");
    }

    /**
     * 岗位修改
     * @param sysPost
     * @return
     */
    @RequestMapping("/update")
    public Result update(@RequestBody SysPost sysPost){
        sysPost.setCreateTime(new Date());
        boolean b = sysPostService.update(sysPost);
        return b?Result.success("操作成功"):Result.fail("操作失败");
    }

    /**
     * 岗位删除 物理删除
     * @param postId
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer postId){
        boolean b = sysPostService.removeById(postId);
        return b?Result.success("删除岗位成功"):Result.fail("删除岗位失败");
    }

    /**
     * 岗位批删
     * @param ids
     * @return
     */
    @RequestMapping("/deletes")
    public Result deletes(String ids){
        List<String> asList = Arrays.asList(ids.split(","));
        sysPostService.deletes(asList);
        return Result.success();
    }

    /**
     * 岗位导出excel
     * @param response
     * @throws IOException
     */
    @RequestMapping("/export")
    public void exportPostExcel(HttpServletResponse response) throws IOException {
        String fileName = URLEncoder.encode("产品信息", "UTF-8").replaceAll("\\+", "%20");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx");
        List<SysPost> list=sysPostService.list();

        EasyExcel.write(response.getOutputStream(),SysPost.class).sheet("产品信息").doWrite(list);

    }
    @Autowired
    SysPostMapper sysPostMapper;

    @RequestMapping("/import")
    public HashMap<String, Object> getimport(MultipartFile file) throws IOException {
        HashMap<String, Object> hashMap = new HashMap<>();

        try {
            EasyExcel.read(file.getInputStream(), SysPost.class, new CellDataDemoHeadDataListenerPost1(sysPostService)).sheet().doRead();
            hashMap.put("msg","成功");
        }catch (Exception e) {
            e.printStackTrace();
            hashMap.put("msg","失败");
        }
        return hashMap;
    }

    @RequestMapping("/addPic")
    public String addPic(MultipartFile file) {
        String s = UploadMinioUtils.uploadPic(file);
        return s;
    }

    @RequestMapping("/postById")
    public Result postById(Integer id){
        SysPost sysPost=sysPostService.postById(id);
        return Result.success(sysPost);
    }

}
